import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/utils/loading/loading_utils.dart';
import 'package:flutter_base/widgets/button/common_button.dart';

class CommonInputBottomDialog extends StatefulWidget {
  const CommonInputBottomDialog({
    super.key,
    this.hintText = "",
    this.defaultText,
    this.sendText,
    this.canEmpty = false,
    this.maxLength = 1000,
    this.maxLine = 1,
    this.inputFormatters,
  });

  final String hintText;
  final String? defaultText;
  final String? sendText;
  final bool canEmpty;
  final int maxLength;
  final int maxLine;
  final List<TextInputFormatter>? inputFormatters;

  @override
  State<CommonInputBottomDialog> createState() => _RewardInputDialogState();
}

class _RewardInputDialogState extends State<CommonInputBottomDialog> {
  late TextEditingController textEditingController;
  bool canSave = false;
  final FocusNode _focusNode = FocusNode();
  bool _sendButtonEnabled = false;

  @override
  void initState() {
    textEditingController = TextEditingController();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (widget.defaultText != null && widget.defaultText!.isNotEmpty) {
        textEditingController.text = widget.defaultText!;
      }
      _focusNode.requestFocus();
    });

    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {}
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      alignment: Alignment.bottomCenter,
      insetPadding: EdgeInsets.zero,
      insetAnimationDuration: const Duration(milliseconds: 0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            bottom: BorderSide(color: Colors.white.withOpacity(0.95), width: 1.w),
          ),
        ),
        constraints: BoxConstraints(maxHeight: 200, minHeight: 76.w),
        padding: EdgeInsets.all(16.w),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Expanded(
              child: Container(
                decoration: const BoxDecoration(
                  color: AppColor.colorEBF0F5,
                  borderRadius: BorderRadius.all(Radius.circular(30)),
                ),
                child: TextField(
                  focusNode: _focusNode,
                  controller: textEditingController,
                  textInputAction: TextInputAction.done,
                  onChanged: (value) {
                    setState(() {
                      _sendButtonEnabled = value.isNotEmpty;
                    });
                  },
                  onSubmitted: (_) {
                    _onTapSend();
                  },
                  maxLength: widget.maxLength,
                  inputFormatters: widget.inputFormatters,
                  maxLines: widget.maxLine,
                  minLines: 1,
                  style: _inputTextStyle(),
                  decoration: InputDecoration(
                    isCollapsed: true,
                    hintText: widget.hintText,
                    contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 11),
                    hintStyle: TextStyle(
                      color: const Color(0xffA3A4AD),
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400,
                    ),
                    counterText: '',
                    border: const OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    enabledBorder: const OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    disabledBorder: const OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                    focusedBorder: const OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.transparent,
                      ),
                    ),
                  ),
                ),
              ),
            ),
            12.horizontalSpace,
            SizedBox(
              width: 72,
              height: 32,
              child: CommonButton(
                title: widget.sendText ?? '发送',
                type: _sendButtonEnabled ? ButtonType.primary : ButtonType.disabled,
                onClick: _sendButtonEnabled ? _onTapSend : null,
              ),
            ).marginOnly(bottom: 2),
          ],
        ),
      ),
    );
  }

  void _onTapSend() {
    if (!widget.canEmpty && textEditingController.text.trim().isEmpty) {
      LoadingUtils.showToast('请输入内容');
      return;
    }
    _focusNode.unfocus();
    Get.back(result: textEditingController.text.trim());
  }

  TextStyle _inputTextStyle() {
    return const TextStyle(
      // color: Color(0xff091326),
      color: Colors.white,
      fontSize: 14,
      fontWeight: FontWeight.w400,
      height: 1.0,
    );
  }
}
